package servlet;

import dao.PersonDAO;
import dao.materialDAO;
import entity.materialBean;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import entity.materialBean;
import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 王开心
 * @Date: 2024/04/21/21:27
 * @Description:
 */
@WebServlet("/group/material/material_save")
public class MaterialSaveServlet  extends HttpServlet {
    public MaterialSaveServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");


        boolean isValid = true;
        String errorMessage = "";

        req.getSession().removeAttribute("name_error");
        req.getSession().removeAttribute("id_error");
        req.getSession().removeAttribute("type_error");
        req.getSession().removeAttribute("manufacturer_error");
        req.getSession().removeAttribute("productiondate_error");
        req.getSession().removeAttribute("factorysite_error");
        req.getSession().removeAttribute("contacts_error");
        req.getSession().removeAttribute("telephone_error");

        materialBean obj=new materialBean();
        obj.setR_id(Integer.parseInt(req.getParameter("r_id")));
        obj.setId(req.getParameter("material_id"));
        obj.setName( req.getParameter("material_name"));
        obj.setId(req.getParameter("material_id"));
        obj.setType( req.getParameter("material_type"));
        obj.setManufacturer(  req.getParameter("material_manufacturer"));
        obj.setProductiondate(req.getParameter("material_productiondate"));
        obj.setFactorysite(   req.getParameter("material_factorysite"));
        obj.setContacts(  req.getParameter("material_contacts"));
        obj.setTelephone(req.getParameter("material_telephone"));
        obj.setStatement(req.getParameter("material_statement"));

        if (obj.getName() == null || obj.getName().isEmpty()) {
            isValid = false;
            req.getSession().setAttribute("name_error", "请填写物料名称");
        }

        if (obj.getId() == null || obj.getId().isEmpty()) {
            isValid = false;
            req.getSession().setAttribute("id_error", "请填写ID");
        }

        if (obj.getType() == null || obj.getType().isEmpty()) {
            isValid = false;
            req.getSession().setAttribute("type_error", "请填写物料类型");
        }

        if (obj.getManufacturer() == null||obj.getManufacturer().isEmpty()) {
            isValid = false;
            req.getSession().setAttribute("manufacturer_error","请填写生产厂商");
        }

        if (obj.getProductiondate()== null ||obj.getProductiondate().isEmpty()) {
            isValid = false;
            req.getSession().setAttribute("productiondate_error","请填写出厂日期");
        }

        if (obj.getFactorysite() == null||obj.getFactorysite().isEmpty() ) {
            isValid = false;
            req.getSession().setAttribute("factorysite_error","请填写厂址");
        }

        if (obj.getContacts() == null || obj.getContacts().isEmpty()) {
            isValid = false;
            req.getSession().setAttribute("contacts_error", "请填写联系人");
        }

        if (obj.getTelephone() == null || obj.getTelephone().isEmpty()) {
            isValid = false;
            req.getSession().setAttribute("telephone_error", "请填写联系电话");
        } else if (!obj.getTelephone().matches("\\d+")) {
            isValid = false;
            req.getSession().setAttribute("telephone_error", "仅填写数字");
        }

        if (!isValid) {
            req.setAttribute("editMaterialBean",obj);
            req.getSession().setAttribute("errorMessage", errorMessage);
            req.getRequestDispatcher("material_edit.jsp").forward(req, resp);
        }else{
            materialDAO dao =new materialDAO();
            if (!String.valueOf(obj.getR_id()).equals("-1")){
                dao.updateMaterial(obj);
            }
            else {
                dao.insertMaterial(obj);
            }
            resp.setHeader("refresh","0;URL="+"material_list");
        }
    }
}
